﻿package PinballCode {
	import flash.display.Sprite;
	
	// Game Ball
	public class Ball extends Sprite {
		
		private var radius:Number;			// radius of the circle
		private var color:uint;				// color of the circle
		private var thickness:Number = 2;	//thickness of the line
		
		// Ball Physics		
		private var ax:Number = 0;			// acceleration in the x
		private var ay:Number = 0;			// acceleration in the y
		public var vx:Number = 0;			// velocity in the x
		public var vy:Number = 0;			// velocity in the y
		
		private var maxV:int = 200;			// max Velocity in a give direction
		
		private var mass:int = 10;		// mass of the ball
		
		private var gravity:Number = 15;	// gravity of ball
		
		public var onPlunger:Boolean = true;		//on plunger
		
		private var friction:Number = .9;		// friction when hit a wall
		
		private var bounce:Number = -0.9;
		private var r:Number = 0;
		private var angV:Number = 0;
		
		
		// Propety
		public function get Radius():Number { return radius;}
		
		
		// Constructor
		public function Ball(radius:Number=10, color:uint=0x00ff00, thickness:Number = 2, maxV:int = 200, mass:int = 100, gravity:Number = 20, friction:Number = .9) {
			this.radius = radius;
			this.color = color;
			this.thickness = thickness;
			this.maxV = maxV;
			this.mass = mass;
			this.gravity = gravity;
			this.friction = friction;
			init();
			reset();
		}
		
		// Initializes the Node
		public function init():void {
			graphics.beginFill(color);
			graphics.lineStyle(thickness,0x000000);
			graphics.drawCircle(0, 0, radius);
			graphics.endFill();
		}
		
		// Resets the ball to start ball
		public function reset():void {
			ax = 0;
			ay = gravity;
			vx = 0;
			vy = 0;
			onPlunger = true;
		}
		
		// Moves the Ball in the X
		public function MoveX(dt:Number):Number {
			ax;
			vx += ax * dt;
			return vx * dt;
		}
		
		// Moves the Ball in the Y
		public function MoveY(dt:Number):Number {
				vy += ay * dt;
				if(vy > maxV)
					vy = maxV;
				else if (vy < -maxV)
					vy = -maxV;
					
			return vy * dt;
		}
		
		// Sum of the forces
		public function sumFY(force:Number):void {
			ay = gravity + force/mass;
			trace("ay: " + ay);
		}
		
		// Bounces the ball off a surface
		public function bounces(angle:Number):void {
			/*
			var V = Math.sqrt(vy*vy +vx * vx);			
			vy = -.9 * V * Math.cos(angle);
			vx = -.9 * V * Math.sin(angle);
			if(vy < gravity + 2 && vy > -(gravity + 2))
				still();
			*/
			if(angle == 0 || angle == Math.PI/2 || angle == (3/2) * Math.PI || angle == 2 * Math.PI)
			{
				vy *= -friction;
				vx *= -friction;
			}
			else
			{
				var tvx= vx;
				var tvy = vy;
				vy = friction * tvx;
				vx = friction * tvy;
			}
		}
		
		// On Plunger and shouldnt move
		public function still():void {
			vx =0;
			vy =0;
			onPlunger = true;
		}
		
		public function checkLine(line:Wall,dt:Number,angular:Boolean = false):Boolean
		{
			// get the bounding box of the line
			if(hitTestObject(line))
			{

				// get angle, sine and cosine
				var angle:Number = line.getAngleAB();
				var cos:Number = Math.cos(angle);
				var sin:Number = Math.sin(angle);
				
				// get position of ball, relative to line
				var x1:Number = x - line.a.x;
				var y1:Number = y - line.a.y;

				// rotate coordinates
				var y2:Number = cos * y1 - sin * x1;
				
				// rotate velocity
				var vy1:Number = cos * vy - sin * vx;
				
				// perform bounce with rotated values
				if(y2 > -height / 2 && y2 < vy1)
				{
					// rotate coordinates
					var x2:Number = cos * x1 + sin * y1;
					
					
					// rotate velocity
					var vx1:Number = cos * vx + sin * vy;
					
					y2 = -height / 2;
					vy1 *= bounce;
					
					if(angular)
					{
						if(line is Bar) {
							var bar = Bar(line) 
							r = Wall.Distance(line.a,new Vertex(x,y));			// distance from pivot point to impact position
							angV = Math.abs(bar.W * r/bar.L) * bar.M/mass;
							trace(angV + "ll");
						}
					}
					else
						angV = 0;
					// rotate everything back;
					x1 = cos * x2 - sin * y2;
					y1 = cos * y2 + sin * x2;
					vx = cos * vx1 - sin * vy1 + angV * cos;
					vy = cos * vy1 + sin * vx1 + angV * sin;
					x = line.a.x + x1;
					y = line.a.y + y1;
					
				}				
				return true;
			}
			return false;
		}
	}
	
}
